package com.cxf.windex.rest.minab;

import com.cxf.windex.base.common.ServiceException;
import com.cxf.windex.base.msg.ObjectRestResponse;
import com.cxf.windex.base.rest.BaseRestController;
import com.cxf.windex.biz.JsUserBiz;
import com.cxf.windex.entity.JsUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author chenxf
 * @date 3/16/2018
 * @description
 */
@Api(tags = {"村广通[小程序-商] - 用户接口"})
@Controller
@RequestMapping("userb/user")
public class UserController extends
        BaseRestController<JsUserBiz, JsUser> {

  @Value("${weixin.appid}")
  private String appid;
  @Value("${weixin.secret}")
  private String secret;

  @ApiOperation(value="登陆", notes="登陆返回TOKEN")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "code", value = "登陆code", required = false, paramType="query", dataType = "String"),
      @ApiImplicitParam(name = "openid", value = "微信openid", required = false,  paramType="query",dataType = "String")
  })
  @RequestMapping(value = "/login", method = RequestMethod.GET)
  @ResponseBody
  public ObjectRestResponse login(String code,String openid){
    Map resultMap=baseBiz.login(code,openid,appid,secret);
    if(resultMap.get("msg")!=null &&StringUtils.isNotBlank(resultMap.get("msg").toString())){
      return  new ObjectRestResponse().rel(false).msg(resultMap.get("msg").toString()).result(resultMap);
    }
    return  new ObjectRestResponse().rel(true).msg("登陆成功").result(resultMap);
  }

  @ApiOperation(value="解密手机号码", notes="解密手机号码")
  @ApiImplicitParams({
          @ApiImplicitParam(name = "encryptedData", value = "微信ncryptedData", required = false, paramType="query", dataType = "String"),
          @ApiImplicitParam(name = "iv", value = "微信iv", required = false,  paramType="query",dataType = "String"),
          @ApiImplicitParam(name = "sessionKey", value = "微信sessionKey", required = false,  paramType="query",dataType = "String")
  })
  @RequestMapping(value = "/phone", method = RequestMethod.POST)
  @ResponseBody
  public ObjectRestResponse decryptPhone(String encryptedData,String iv,String sessionKey) throws Exception{
    return  new ObjectRestResponse().rel(true).msg("解密成功").result(baseBiz.decryptPhone(encryptedData,iv,sessionKey,this.getCurrentUserId()));
  }

  @ApiOperation(value="初始化支付密码", notes="初始化支付密码")
  @ApiImplicitParams({
          @ApiImplicitParam(name = "password", value = "支付密码", required = false,  paramType="query",dataType = "String"),
          @ApiImplicitParam(name = "openid", value = "微信openid", required = false,  paramType="query",dataType = "String")
  })
  @RequestMapping(value = "/initPassword", method = RequestMethod.POST)
  @ResponseBody
  public ObjectRestResponse initPassword(String password,String openid){
    baseBiz.setPassword(openid,password,null);
    return  new ObjectRestResponse().rel(true).msg("初始化成功");
  }

  @ApiOperation(value="旧密码修改支付密码", notes="旧密码修改支付密码")
  @ApiImplicitParams({
          @ApiImplicitParam(name = "oldPassword", value = "旧支付密码", required = false,  paramType="query",dataType = "String"),
          @ApiImplicitParam(name = "password", value = "支付密码", required = false,  paramType="query",dataType = "String"),
          @ApiImplicitParam(name = "openid", value = "微信openid", required = false,  paramType="query",dataType = "String")
  })
  @RequestMapping(value = "/resetPassword", method = RequestMethod.POST)
  @ResponseBody
  public ObjectRestResponse resetPassword(String oldPassword,String password,String openid){
    if(StringUtils.isBlank(oldPassword)){
      throw new ServiceException("请填写旧支付密码");
    }
    baseBiz.setPassword(openid,password,oldPassword);
    return  new ObjectRestResponse().rel(true).msg("设置成功");
  }


  @ApiOperation(value="注册", notes="注册返回TOKEN")
  @RequestMapping(value = "/register", method = RequestMethod.POST)
  @ResponseBody
  public ObjectRestResponse register(@RequestBody JsUser entity){
    return  new ObjectRestResponse().rel(true).msg("注册成功").result(baseBiz.register(entity));
  }

  @ApiOperation(value="支付结果", notes="返回支付结果")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "return_msg", value = "返回信息", required = false,  paramType="query",dataType = "String"),
      @ApiImplicitParam(name = "result_code", value = "支付结果", required = false,  paramType="query",dataType = "String"),
      @ApiImplicitParam(name = "openid", value = "微信openid", required = false,  paramType="query",dataType = "String")
  })
  @RequestMapping(value = "/pay", method = RequestMethod.POST)
  @ResponseBody
  public ObjectRestResponse pay(String return_msg,String result_code,String openid){
    baseBiz.pay(return_msg,result_code,openid);
    return  new ObjectRestResponse().rel(true).msg("支付成功");
  }


}
